/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client.dialogs;

import com.google.inject.Inject;
import listo.client.AppConfig;
import listo.client.ImageProvider;
import listo.client.MainForm;
import listo.utils.MiscUtils;
import listo.utils.swing.HyperlinkLabel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class AboutDialog extends JDialog {

    private final MainForm mainForm;
    private final ImageProvider imageProvider;
    private final String link;
    private final String version;
    private final String buildTime;

    private HyperlinkLabel linkLabel;

    @Inject
    public AboutDialog(MainForm mainForm, ImageProvider imageProvider, AppConfig appConfig) {
        this.mainForm = mainForm;
        this.imageProvider = imageProvider;
        this.link = appConfig.getAboutLink();
        this.version = appConfig.getVersion();
        this.buildTime = appConfig.getBuildTime();
    }

    public void run() {
        createUI();

        addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                dispose();
            }
        });

        addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e) {
                dispose();
            }
        });

        linkLabel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                MiscUtils.openBrowser(link);
                dispose();
            }
        });

        setModal(true);
        setResizable(false);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        pack();
        setLocationRelativeTo(mainForm);
        setVisible(true);
    }

    private void createUI() {
        JPanel contentPane = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
        ImageIcon image = imageProvider.getIcon("splash.png");
        JLabel imageLabel = new JLabel(image);
        contentPane.add(imageLabel);

        linkLabel = new HyperlinkLabel(link);
        linkLabel.setFont(linkLabel.getFont().deriveFont(Font.BOLD));
        linkLabel.setSize(linkLabel.getPreferredSize());
        linkLabel.setLocation(10, 300);
        getLayeredPane().add(linkLabel);

        JLabel versionLabel = new JLabel("Version " + version);
        versionLabel.setSize(versionLabel.getPreferredSize());
        versionLabel.setLocation(10, 320);
        getLayeredPane().add(versionLabel);

        JLabel buildTimeLabel = new JLabel("Built on " + buildTime);
        buildTimeLabel.setSize(buildTimeLabel.getPreferredSize());
        buildTimeLabel.setLocation(10, 335);
        getLayeredPane().add(buildTimeLabel);

        setContentPane(contentPane);
    }

}
